New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

promisfy

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promisfy

transform a node-style asynchronous function to promise-style function, very handy for async/await

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1K
decreased by-12.17%
Maintainers
1
Weekly downloads
 
Created
Source

promisfy

description

this package is used for transform a node-style asynchronous function to a promise-style function. It's very handy if you are using async/await. After v1.1.0, you can also use it for a event callback.

a node-style asynchronous function should be like this:

function asycFunction(arg1, arg2, callback) {
    // do something
}

// callback should be like this
function callback(err, result) {
    // do something
}

The first argument err represents whether the asynchronous call is failed(null while it's successful), and the second argument is the result of this call.

install

npm install --save promisfy

usage

very simple to use:

const fs = require('fs');
const http = require('http');
const {promisfy, waitFor} = require('promisfy');

// using promisfy
// if you are using some some callbacks without error as its first argument,
// try promisfyNoError()
const readFile = promisfy(fs.readFile);

async function main() {
    let content = await readFile('myfile.txt', {encoding:'utf8'});

    return content;
}

main().then(function(content) {
    console.log('myfile:');
    console.log(content);
})

// using waitFor
// receive post data
http.createServer(80, function(req, res) {
    async function handleRequest(req, res) {
        if (req.method === 'POST') {
            req.body = await waitFor(req.sock, 'data');
        }

        // now you can do something with req.body
    }
})

Be attention, waitFor the data or stream event will make this function add listener to data, end and error events. All data will be returned only if there is no error event triggered and the end event is triggered. As for other event, only the second argument will be passed to the Promise

After v1.1.4, you can pass a context to the promisfy as its second argument. Context will be used as the context of fn, for example:

function callback() {
    console.log(this)
}

promisfy(fs.readFile, fs);

If your callback function expects more than just 2 arguments, the 2nd through nth arguments will be automatically bundled in an array when the promise resolves.

foo(inputArg1, (error,responseArg1,responseArg2,responseArg3) => {})

can be promisfied like this:

var [responseArg1,responseArg2,responseArg3] = await promisfy(foo)(inputArg1)

Keywords

FAQs

Package last updated on 14 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc